<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :md="7" :sm="12">
            <a-form-item label="商品类型">
              <!--              <a-input placeholder="请输入面料编号" v-model="queryParam.code" style="width: 100%"></a-input>-->
              <a-select placeholder="请选商品类型" style="width: 100%" v-model="queryParam.typeid">
                <a-select-option v-for="item in typeList" :key="item.id" :value="item.id">
                  {{ item.goodsTypeName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="7" :sm="12">
            <a-form-item label="系列名称">
              <a-input placeholder="请输入系列名称" v-model="queryParam.name" style="width: 100%"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="7" :sm="12">
            <a-form-item label="销售状态">
              <a-select placeholder="请选择销售状态" style="width: 100%" v-model="queryParam.status">
                <a-select-option value="0">
                  上架中
                </a-select-option>
                <a-select-option value="2">
                  已下架
                </a-select-option>
                <!--                <a-select-option value="1">
                                  已删除
                                </a-select-option>-->
              </a-select>
            </a-form-item>
          </a-col>


        </a-row>

        <a-row :gutter="24">
          <a-col :xl="3" :lg="7" :md="4" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a-button type="primary" @click="showModal" style="margin-left: 8px">新增系列</a-button>
            </span>
          </a-col>
        </a-row>
        <!--新增系列弹框-->
        <a-modal
          title="新增系列"
          v-model:visible="visible"
          :confirm-loading="confirmLoading"
          @ok="addGroup"
          @close="cleatDate"
        >
          <a-form :label-col="labelCol" :wrapper-col="wrapperCol">

            <a-form-item label="商品类型">
              <!--                <a-input v-model="goodsTypeid" placeholder="商品类型"></a-input>-->
              <a-select placeholder="请选商品类型" style="width: 100%" v-model="goodsTypeid">
                <a-select-option v-for="item in typeList" :key="item.id" :value="item.id">
                  {{ item.goodsTypeName }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="系列名称">
              <a-input v-model="groupName" placeholder="系列名称"></a-input>
            </a-form-item>
            <a-form-item label="系列单价">
              <a-input style="width: 100%" v-model="GroupPrice" placeholder="系列单价"></a-input>
            </a-form-item>
            <a-form-item label="备注信息">
              <a-textarea v-model="remake" placeholder="备注" :rows="4"></a-textarea>
            </a-form-item>
          </a-form>
        </a-modal>
        <!--新增系列弹框结束-->

        <!--编辑组的弹窗-->
        <a-modal
          title="编辑商品系列"
          v-model:visible="visible2"
          :confirm-loading="confirmLoading"
          @ok="updateGroup"
          @close="cleatDate"
        >
          <a-form :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-form-item label="商品类型">
              <!--                <a-input v-model="goodsTypeid" placeholder="商品类型"></a-input>-->
              <a-select placeholder="请选商品类型" style="width: 100%" v-model="goodsTypeid">
                <a-select-option v-for="item in typeList" :key="item.id" :value="item.id.toString()">
                  {{ item.goodsTypeName }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="系列名称">
              <a-input v-model="groupName" placeholder="系列名称"></a-input>
            </a-form-item>
            <a-form-item label="系列单价">
              <a-input style="width: 100%" v-model="GroupPrice" placeholder="系列单价"></a-input>
            </a-form-item>
            <a-form-item label="备注信息">
              <a-textarea v-model="remake" placeholder="备注" :rows="4"></a-textarea>
            </a-form-item>
          </a-form>
        </a-modal>
        <!--编辑弹框结束-->

      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!--table区域-begin-->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>

        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="30px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-menu-item v-if="record.status != '1'" style="display: inline;margin-left: 10px">
            <a @click="gotoFabricList(record.id)">商品管理</a>
          </a-menu-item>
          <a-menu-item v-if="record.status != '1'" style="display: inline;margin-left: 10px">
             <a @click="$router.push({path:'/goodsCenter/addGroupPrice',query:{group:record.id}})">阶梯售价</a>
          </a-menu-item>
          <a-menu-item v-if="record.status != '1'" style="display: inline;margin-left: 10px">
            <a @click="GroupDetail(record)">编辑</a>
          </a-menu-item>
          <a-menu-item v-if="record.status != '1'" style="display: inline;margin-left: 10px">
                  <a-popconfirm title="确定删除吗?" @confirm="() => groupdeleteGroup(record.id)">
                    <a style="color:deepskyblue">删除</a>
                  </a-popconfirm>
          </a-menu-item>
          <a-menu-item v-if="record.status=='0' && record.status != '1'" style="display: inline;margin-left: 10px">
                  <a-popconfirm title="确定下架吗?" @confirm="() => groupShelves(record.id)">
                    <a style="color:deepskyblue">下架</a>
                  </a-popconfirm>
          </a-menu-item>

          <!--          <a-menu-item v-if="record.status == '1'" style="display: inline;margin-left: 10px">
                            <a-popconfirm title="确定恢复吗?" @confirm="() => huifuFunc(record.id)">
                              <a style="color:deepskyblue">恢复</a>
                            </a-popconfirm>
                    </a-menu-item>-->

          <a-menu-item v-if="record.status=='2'" style="display: inline;margin-left: 10px">
                  <a-popconfirm title="确定上架吗?" @confirm="() => groupUpFunc(record.id)">
                    <a style="color:deepskyblue">上架</a>
                  </a-popconfirm>
          </a-menu-item>

        </span>
      </a-table>
    </div>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import JDate from '@/components/jeecg/JDate.vue'
import {getAction} from "../../api/manage";

export default {
  name: 'Fabric',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDate
    // LgyxCreditRateModal
  },
  data() {
    return {
      typeList: [], // 类型列表
      goodsTypeid: '',// 选中的商品类型id
      labelCol: {span: 4},
      wrapperCol: {span: 14},
      groupName: '', // 组名称
      GroupPrice: '',// 组价格
      groupID: '', // 选中的组ID
      remake: '',// 组备注
      visible: false, // 新增弹框的显示
      visible2: false, // 编辑弹框的显示
      description: '面料订单',
      fabricOrderDetail: [], // 当前的面料订单详情
      orderDetail: {}, // 订单详情
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '系列名称',
          align: "center",
          dataIndex: 'seriesName'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime'
          /* customRender: (text) => {
             //字典值替换通用方法
             return filterDictTextByCache('sex', text);
           }*/
        },
        {
          title: '零售单价',
          align: "center",
          dataIndex: 'retailPrice'
        },
        {
          title: '货号数量',
          align: "center",
          dataIndex: 'count'
        },
        {
          title: '销售状态',
          align: "center",
          dataIndex: 'status',
          customRender: function (text) {
            if (text == '0') {
              return '上架中'
            } else if (text == '2') {
              return '已下架'
            } else if (text == '1') {
              return '已删除'
            }
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          // fixed:"right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: '/goodsCenter/listGoodsGroup',
        addGoodGroup: '/goodsCenter/addGoodGroup', // 添加商品系列
        updateGoodGroup: '/goodsCenter/updateGoodGroup', // 更新商品系列
        offGoodGroup: '/goodsCenter/offGoodGroup', // 下架商品系列
        upGoodGroup: '/goodsCenter/upGoodGroup', // 上架商品系列
        delGoodGroup: '/goodsCenter/delGoodGroup', // 删除商品系列
        laGoodsType: '/goodsCenter/laGoodsType', // 商品类型，用于下拉列表


        /*addGroup: '/fabricCenter/addGroup', // 添加面料组
        updateGroup: '/fabricCenter/updateGroup', // 更新面料组
        shelves: '/fabricCenter/shelves', // 面料组下架
        groupUp: '/fabricCenter/groupUp', // 面料组上架
        deleteGroup: '/fabricCenter/deleteGroup', // 面料组下架
        huifu: '/fabricCenter/huifu' // 面料组恢复下架状态*/
      },
      dictOptions: {},
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  created() {
    this.laGoodsTypeFunc()
  },
  methods: {

    /**
     * 获取商品类型下拉列表
     */
    laGoodsTypeFunc() {
      getAction(this.url.laGoodsType).then(res => {
        if (res.code == '0') {
          this.typeList = res.data
        }
      })
    },

    gotoFabricList(groupID) { // 页面跳转
      this.$router.push({
        path: '/goodsCenter/GoodsList',
        query: {
          group: groupID
        }
      })
    },

    // 面料组删除
    groupdeleteGroup(groupID) {
      getAction(this.url.delGoodGroup,
        {
          'id': groupID
        }).then(res => {
        this.cleatDate()
        if (res.code == '200') {
          this.$message.success("删除成功")
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },


    huifuFunc(groupID) {
      getAction(this.url.huifu, {'groupID': groupID}).then(res => {
        this.cleatDate()
        if (res.code == '200') {
          this.$message.success("恢复成功")
          this.loadData()
        } else {
          this.$message.error("恢复失败")
        }
      })
    },

    // 面料组下架
    groupShelves(groupID) {
      getAction(this.url.offGoodGroup,
        {
          'id': groupID
        }).then(res => {
        this.cleatDate()
        if (res.code == '200') {
          this.$message.success("下架成功")
          this.loadData()
        } else {
          this.$message.error("下架失败")
        }
      })
    },
    /**
     * 面料组上架
     */
    groupUpFunc(groupID) {
      getAction(this.url.upGoodGroup,
        {
          'id': groupID
        }).then(res => {
        this.cleatDate()
        if (res.code == '200') {
          this.$message.success("上架成功")
          this.loadData()
        } else {
          this.$message.error("上架失败")
        }
      })
    },

    // 点击编辑先查详情
    GroupDetail(record) {
      this.goodsTypeid = record.goodsTypeId
      this.groupID = record.id
      this.groupName = record.seriesName
      this.GroupPrice = record.retailPrice
      this.remake = record.remark
      this.visible2 = true
    },

    // 更新面料组
    updateGroup() {
      this.visible2 = false
      getAction(this.url.updateGoodGroup,
        {
          'id': this.groupID,
          'goodsTypeId': this.goodsTypeid,
          'seriesName': this.groupName,
          'retailPrice': this.GroupPrice,
          'remark': this.remake
        }
      ).then(res => {
        this.cleatDate()
        if (res.code == '200') {
          this.$message.success("修改成功")
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    showModal() { // 新增面料组弹框现实
      this.cleatDate()
      this.visible = true;
    },
    // 新增商品系列
    addGroup() {
      if (this.goodsTypeid == '') {
        this.$message.warning("请选择商品类型")
        return;
      } else if (this.groupName == '') {
        this.$message.warning("请填写商品系列名称")
        return
      } else if (this.GroupPrice == '') {
        this.$message.warning("请填写商品系列单价")
        return;
      } else {
        getAction(this.url.addGoodGroup,
          {
            'goodsTypeId': this.goodsTypeid,
            'seriesName': this.groupName,
            'retailPrice': this.GroupPrice,
            'remark': this.remake
          }).then(res => {
          this.cleatDate()
          if (res.code == '200') {
            this.$message.success("添加成功")
            this.loadData()
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    cleatDate() { // 添加组之后清楚数据
      this.groupName = ''
      this.GroupPrice = ''
      this.remake = ''
      this.visible = false
      this.goodsTypeid = ''
    },
    initDictConfig() {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

.fabricName {
  margin-left: 10px;
  margin-top: 10px;
  width: auto;
  display: block !important;
  background: #2d2d2d;
  color: #ffe56d;
  text-align: center;
  border-radius: 3px;
}

.fabigtitle {
  height: 34px;
  width: 200px;
  text-align: left;
}

.fabigcontent {
  height: 34px;
  width: 200px;
  text-align: right;
}

.fatitle {
  height: 34px;
  width: 82px;
  background: rgb(242 242 242);
  text-align: center;
}

.facontent {
  width: 180px;
  overflow: hidden;
  text-align: center;
}
</style>